package com.red.wood.module.cdn.mybatis.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.red.wood.common.domain.Page;
import com.red.wood.common.springjdbc.SqlServerDao;
import com.red.wood.common.util.CommonUtils;
import com.red.wood.common.util.IMap;
import com.red.wood.module.cdn.mybatis.dao.CdnDataDao;

@Service
public class CdnDataService {

	@Autowired
	private CdnDataDao dao;
	
	@Autowired
	private SqlServerDao sqlServerDao;
	
	public Map<String, Object> getPageData(IMap param) {
		Page page = (Page) param.get("page");
		int offset = (page.getPageNo() - 1) * page.getPageSize();
		
		IMap map = sqlServerDao.getUserLevelInfo(param.getString("userId"));
		
		String userProvince = CommonUtils.getLoginUserProvince(CommonUtils.valueOf(map.get("loginUser")));
		String provinceId = CommonUtils.getProvinceId(userProvince);
		int pageSize = page.getPageSize();
		param.put("userLevel", map.get("userLevel"));
		param.put("userProvince", userProvince);
		param.put("provinceId", provinceId);
		param.put("pageSize", pageSize);
		param.put("offset", offset);
		
		long count = 0;
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		
		count = dao.getCount(param);
		list = filter(dao.getPageData(param));
		
		Map<String, Object> rs = new HashMap<String, Object>();
		rs.put("totalCount", count);
		rs.put("pageData", list);
		return rs;
	}
	
	public List<Map<String,Object>> exportData(IMap param){
		String userProvince = CommonUtils.getLoginUserProvince(CommonUtils.valueOf(param.get("loginUser")));
		String provinceId = CommonUtils.getProvinceId(userProvince);
		param.put("userProvince", userProvince);
		param.put("provinceId", provinceId);
		
		List<Map<String, Object>> list = filter(dao.exportData(param));
		
		return list;
	}
	
	public Map<String, Object> getDetailInfo(String cdnId) {
		List<Map<String,Object>> list = dao.getDetailInfo(cdnId);
		Map<String,Object> map = new HashMap<String,Object>();
		if(list != null && list.size() > 0) {
			map = list.get(0);
		}
		return map;
	}
	
	public List<Map<String, Object>> filter(List<Map<String, Object>> list){
		if(list != null && list.size() > 0) {
			for(Map<String,Object> map : list) {
				map.put("subNum",CommonUtils.filteNull(map.get("subNum")));
				map.put("ipNum",CommonUtils.filteNull(map.get("ipNum")));
				map.put("customerNum",CommonUtils.filteNull(map.get("customerNum")));
				map.put("domainNum",CommonUtils.filteNull(map.get("domainNum")));
				map.put("houseNum",CommonUtils.filteNull(map.get("houseNum")));
				map.put("nodeNum",CommonUtils.filteNull(map.get("nodeNum")));
			}
		}
		return list;
	}
}
